<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <todo-header @getname="addList" :arr="list"></todo-header>
    <todo-main :arr="showArr" @del="delFn"></todo-main>
    <todo-footer
      :arr="showArr"
      @changeType="changeFn"
      @clearType="clearFn"
    ></todo-footer>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

import "./styles/base.css";
import "./styles/index.css";
export default {
  data() {
    return {
      cons: "all", //要显示的数据类型字符串
      list: JSON.parse(localStorage.getItem("todolist")) || [],
    };
  },
  methods: {
    addList(thename) {
      this.list.push({
        id:
          this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name: thename,
        isDone: false,
      });
    },
    delFn(index) {
      this.list.splice(index, 1);
    },
    changeFn(conStr) {
      this.cons = conStr;
    },
    clearFn() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  computed: {
    showArr() {
      if (this.cons === "ok") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.cons === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler(newVal) {
        localStorage.setItem("todolist", JSON.stringify(newVal));
      },
      deep: true,
    },
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>